package com.ruoyi.system.service.impl;

import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.system.domain.SectorSpot;
import com.ruoyi.system.domain.Stocklist;
import com.ruoyi.system.mapper.SectorSpotMapper;
import com.ruoyi.system.service.ISectorSpotService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SectorDetailMapper;
import com.ruoyi.system.domain.SectorDetail;
import com.ruoyi.system.service.ISectorDetailService;
import com.ruoyi.common.core.text.Convert;

/**
 * 板块详情Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-12-27
 */
@Service
public class SectorDetailServiceImpl  implements ISectorDetailService
{
    @Autowired
    private SectorDetailMapper sectorDetailMapper;
    @Autowired
    private ISectorSpotService sectorSpotService;
    /**
     * 查询板块详情
     * 
     * @param id 板块详情主键
     * @return 板块详情
     */
    @Override
    public SectorDetail selectSectorDetailById(Long id)
    {
        return sectorDetailMapper.selectSectorDetailById(id);
    }
    @Override
    public SectorDetail getOne(SectorDetail stocklist) {
        return Optional.ofNullable(this.selectSectorDetailList(stocklist)).map(stocklists ->{
            if (stocklists.size() > 0) {
                return stocklists.get(0);
            }
            return null;
        }).orElse(null);

    }
    /**
     * 查询板块详情列表
     * 
     * @param sectorDetail 板块详情
     * @return 板块详情
     */
    @Override
    public List<SectorDetail> selectSectorDetailList(SectorDetail sectorDetail)
    {
        return sectorDetailMapper.selectSectorDetailList(sectorDetail);
    }

    @Override
    public List<SectorDetail> selectNowDateList() {
        List<SectorDetail> list = selectSectorDetailList(new SectorDetail());
        if (list!=null && list.size()>0){
            Date date = list.stream().limit(1).collect(Collectors.toList()).get(0).getDate();
            list = list.stream()
                    .filter(sectorDetail1 -> sectorDetail1.getDate().equals(date))
                    .sorted(Comparator.comparing(SectorDetail::getChangepercent).reversed())
                    .map(sectorDetail1 ->{
                        sectorDetail1.setName(sectorSpotService.getOne(new SectorSpot(sectorDetail1.getLabel())).getType());
                        return sectorDetail1;
                    }).collect(Collectors.toList());
        }
        return list;
    }

    /**
     * 新增板块详情
     * 
     * @param sectorDetail 板块详情
     * @return 结果
     */
    @Override
    public int insertSectorDetail(SectorDetail sectorDetail)
    {
        return sectorDetailMapper.insertSectorDetail(sectorDetail);
    }

    /**
     * 修改板块详情
     * 
     * @param sectorDetail 板块详情
     * @return 结果
     */
    @Override
    public int updateSectorDetail(SectorDetail sectorDetail)
    {
        return sectorDetailMapper.updateSectorDetail(sectorDetail);
    }

    /**
     * 批量删除板块详情
     * 
     * @param ids 需要删除的板块详情主键
     * @return 结果
     */
    @Override
    public int deleteSectorDetailByIds(String ids)
    {
        return sectorDetailMapper.deleteSectorDetailByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除板块详情信息
     * 
     * @param id 板块详情主键
     * @return 结果
     */
    @Override
    public int deleteSectorDetailById(Long id)
    {
        return sectorDetailMapper.deleteSectorDetailById(id);
    }
}
